import { Global } from "../../Game/info/Global";
import MusicManager from "../../MusicManager";
import Scene from "../../Scene";
import { SceneState, TButton, TLayout } from "../../SceneControl";
import MainControl from "./MainControl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StoreScene extends Scene {

    @property(cc.Prefab)
    propItem: cc.Node = null;

    @property(cc.Node)
    propItemParent: cc.Node = null;

    @property([cc.SpriteFrame])
    propIcons: cc.SpriteFrame[] = [];

    onEnable() {
        super.onEnable();
        this.initScene();
    }

    public initScene() {
        let layout = new TLayout();
        let btns: TButton[][] = [];

        let scorll = this.node.getComponentInChildren(cc.ScrollView);
        for (let i = 0; i < this.propItemParent.children.length; i++) {
            let item = this.propItemParent.children[i];
            let row = Math.floor(i / 2);
            if (!btns[row]) btns[row] = [];
            let tbtn = new TButton(item.getChildByName("blueBtn"));

            tbtn.addBeforeFingerInEvent((para) => {
                scorll.scrollToPercentVertical(1 - (para.targetLayout.site.row / (para.targetLayout.buttonList.length - 1)), 0.1);
            })
            tbtn.offset = cc.v3(60, 0);
            btns[row].push(tbtn);
        }
        layout.buttonList = btns;

        this.tscene.addLayout(layout);
        this.tscene.onBack = () => {
            this.tscene.sceneState = SceneState.stop;
            this.tscene.apply();
            cc.tween(this.node)
                .to(0.2, { y: -690 })
                .set({ active: false })
                .call(() => this.node.destroy())
                .start();
        }
        this.tscene.joinController();
    }

    protected onLoad(): void {
        let index = 0;
        for (const key in Global.PropInfo) {
            this.refreshPropItem(null, Global.PropInfo[key], index++);
        }
    }

    refreshPropItem(propNode: cc.Node, propInfo: { 名字: string, 说明: string, 价格: number }, index: number) {
        if (!propNode) {
            propNode = cc.instantiate(this.propItem);
            this.propItemParent.addChild(propNode);
        }
        propNode.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.propIcons[index];
        propNode.getChildByName("name").getComponent(cc.Label).string = propInfo.名字;
        propNode.getChildByName("infor").getComponent(cc.Label).string = propInfo.说明;
        propNode.getChildByName("count").getComponent(cc.Label).string = Global.gameInfo.propCount[index].toString();
        cc.find("blueBtn/price", propNode).getComponent(cc.Label).string = propInfo.价格.toString();

        let btn = propNode.getChildByName("blueBtn").getComponent(cc.Button);
        btn.interactable = Global.gameInfo.coin >= propInfo.价格;
        btn.node.off("click");
        btn.node.on('click', () => {
            MusicManager.instance.play(MusicManager.instance.btnMove);
            Global.gameInfo.coin -= propInfo.价格;
            Global.gameInfo.propCount[index]++;
            Global.saveGameInfo();
            cc.find("Canvas").getComponent(MainControl).coinLabel.string = Global.gameInfo.coin.toString();
            this.refresh();
        })
    }

    refresh() {
        let index = 0;
        for (const key in Global.PropInfo) {
            this.refreshPropItem(this.propItemParent.children[index], Global.PropInfo[key], index++);
        }
    }
}
